package com.xuecheng.content.api;

import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeacherDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Result;
import com.xuecheng.content.model.po.Teachers;
import com.xuecheng.content.service.TeachplanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Mr.M
 * @version 1.0
 * @description 课程计划管理相关的接口
 * @date 2023/2/14 11:25
 */
@Api(value = "课程计划编辑接口",tags = "课程计划编辑接口")
@RestController
public class TeachplanController {

    @Autowired
    TeachplanService teachplanService;

   @ApiOperation("查询课程计划树形结构")
   //查询课程计划  GET /teachplan/22/tree-nodes
   @GetMapping("/teachplan/{courseId}/tree-nodes")
 public List<TeachplanDto> getTreeNodes(@PathVariable Long courseId){
       List<TeachplanDto> teachplanTree = teachplanService.findTeachplanTree(courseId);
       return teachplanTree;
   }

    @ApiOperation("课程计划创建或修改")
    @PostMapping("/teachplan")
    public void saveTeachplan( @RequestBody SaveTeachplanDto teachplan){
        teachplanService.saveTeachplan(teachplan);
    }

    @ApiOperation(value = "课程计划和媒资信息绑定")
    @PostMapping("/teachplan/association/media")
    public void associationMedia(@RequestBody BindTeachplanMediaDto bindTeachplanMediaDto){
        teachplanService.associationMedia(bindTeachplanMediaDto);
    }

    @ApiOperation(value = "课程计划删除")
    @DeleteMapping("/teachplan/{id}")
    public Result deleteCourse(@PathVariable Long id){
       Integer flag = teachplanService.deleteCouse(id);
       if(flag==1) {
           return null;
       }
       String code="120409";
       String message="课程计划信息还有子级信息，无法操作";
       Result result=new Result();
       result.setCode(code);
       result.setMessage(message);
       return result;
    }
    @ApiOperation(value = "下移课程计划")
    @PostMapping("/teachplan/movedown/{id}")
    public void movedownTeachplan(@PathVariable Long id){
       teachplanService.modedownTeachplan(id);
    }

    @ApiOperation(value = "上移课程计划")
    @PostMapping("/teachplan/moveup/{id}")
    public void moveupTeachplan(@PathVariable Long id){
       teachplanService.moveupTeachplan(id);
    }

    @ApiOperation(value = "教师查询")
    @GetMapping("/courseTeacher/list/{id}")
    public List<Teachers> getAllTeachers(@PathVariable Long id){
        List<Teachers> teachersList = teachplanService.getTeachersByCourseId(id);
        return teachersList;
    }

    @ApiOperation(value = "添加教师")
    @PostMapping("/courseTeacher")
    public Teachers saveTeacher(@RequestBody TeacherDto teacherDto){
        Teachers teachers=new Teachers();
        BeanUtils.copyProperties(teacherDto,teachers);
        Long id=teachplanService.saveTeacher(teacherDto);
        teachers.setId(id);
        System.out.println("返回的数据======>"+teachers);
        return teachers;
    }

    @ApiOperation(value = "修改教师")
    @PutMapping("/courseTeacher")
    public Teachers updateTeacher(@RequestBody TeacherDto teacherDto){
       Teachers teachers=new Teachers();
       BeanUtils.copyProperties(teacherDto,teachers);
       teachplanService.updateTeacher(teachers);
       return teachers;
    }

    @ApiOperation(value = "删除教师")
    @DeleteMapping("/courseTeacher/course/{courseId}/{teacherId}")
    public void deleteTeacher(@PathVariable Long courseId,@PathVariable Long teacherId){
       teachplanService.deleteTeacher(courseId,teacherId);
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping("/course/{courseId}")
    public void deleteTeachPlan(@PathVariable Long courseId){
       teachplanService.deleteTeachPlan(courseId);
    }
}
